<template>
  <div class="serch flex">
    <c-header :active="type" @changeType="getType"></c-header>
    <div class="title">搜索结果</div>
    <div class="main" style="position: relative;">
      <div class="padding">
        <div class="about-search">
          <form action="">
            <input name="city" type="text" class="city-name" placeholder="关键词搜索" v-model="keywords" @keyup.enter.native="searchFun">
            <button @click.prevent="searchFun" type="submit" class="search-store">搜索</button>
          </form>
        </div>
      </div>
      <div class="serch-list">
        <tab :line-width="2" active-color="#000" v-model="index">
          <tab-item class="vux-center" :selected="selected === index" v-for="(item, index) in searchTabArr" :key="index"
            @on-item-click="getTabList(index, item.id)">{{item.label}}</tab-item>
        </tab>
        <div class="tab-swiper vux-center">
          <div class="serch-column" v-show="index == 0">
            <div class="news-li" v-for="(item, index) in searchList.news" :key="index">
              <router-link :to="{path: '/news-details', query:{ id: item.id }}">
                <div class="news-li-hd">
                  <img :src="item.img_url" :alt="item.title" />
                </div>
                <div class="news-li-bd">
                  <p class="news-li-bd_title">{{item.title}}</p>
                  <p class="news-li-bd_desc">{{item.tags}}</p>
                </div>
              </router-link>
            </div>
            <div class="tips" v-show="searchList.news && searchList.news.length === 0">暂无信息</div>
          </div>
          <div class="serch-column" v-show="index == 1">
            <div class="product-list" v-show="searchList.product && searchList.product.length > 0">
              <div class="product-type-sign">
                <div><span class="black"></span><span>殿堂版</span></div>
                <div><span class="orange"></span><span>典藏版</span></div>
                <div><span class="green"></span><span>乐活版</span></div>
                <div><span class="stock"></span><span>有库存</span></div>
              </div>
              <div class="product-li" v-for="(item, index) in searchList.product" :key="index">
                <img class="product-img" :src="item.imgurl" :alt="item.product_name">
                <p class="product-desc">
                  <span class="wid">{{item.product_name}}</span>
                  <img class="shoppingcart" src="@/assets/img/shoppingcart.png" alt="" v-if="type == 1" @click.stop="addCart(item.id)">
                </p>
                <p><span :class="classObject(item.version)"></span><span>{{item.code}}</span><span v-if="item.stock == 1"
                    class="stock"></span></p>
              </div>
            </div>
            <div class="tips" v-show="searchList.product && searchList.product.length === 0">暂无信息</div>
          </div>
          <div class="serch-column" v-show="index == 2">
            <div :class="{'case-list': searchList.inspiration && searchList.inspiration.length > 0}">
              <div class="case-list-li" v-for="(item, index) in searchList.inspiration" :key="index">
                <router-link :to="{path: '/case-details', query: {id: item.id}}"><img :src="item.img_url" alt="" class="case-li_img"></router-link>
                <p class="case-li_title">{{ item.title }}</p>
              </div>
            </div>
            <div class="tips" v-show="searchList.inspiration && searchList.inspiration.length === 0">暂无信息</div>
          </div>
          <div class="serch-column dealer-list" v-show="index == 3">
            <chanse-file :downloadFile="searchList.downloadFile" v-if="hasLogin"></chanse-file>
            <div class="tips" v-else>请前往<router-link :to="{path: '/dealer', query: { type: type }}">经销商中心</router-link>登录！</div>
          </div>
        </div>
      </div>
    </div>
    <c-footer :type="type" @changeType="getType"></c-footer>
  </div>
</template>

<script>
  import {
    search
  } from '@/api'
  import {
    Tab,
    TabItem
  } from "vux";
  import ChanseFile from "@/components/ChanseFile/ChanseFile";
  export default {
    components: {
      Tab,
      TabItem,
      ChanseFile
    },
    data() {
      return {
        type: this.$route.query.type ? this.$route.query.type : 0,
        keywords: this.$route.query.keywords,
        index: 0,
        selected: 0,
        searchTabArr: [{
          id: 0,
          label: '最新动态'
        }, {
          id: 1,
          label: '产品中心'
        }, {
          id: 2,
          label: '案例灵感'
        }, {
          id: 3,
          label: '经销商中心'
        }],
        searchList: []
      }
    },
    created() {
      this.searchFun()
    },
    computed: {
      hasLogin() {
        let token = sessionStorage.getItem("token");
        return token ? true : false
      },
      classObject: function() {
        return function(version) {
          return {
            black: version == 1,
            orange: version == 2,
            green: version == 3,
          }
        }
      }
    },
    methods: {
      getType(type) {
        this.type = type
      },
      searchFun() {
        search({
          keywords: this.keywords
        }).then(res => {
          if (res.code == 200) {
            this.searchList = res.data
          }
        })
      },
      getTabList() {}
    },
  }
</script>

<style lang="less" scoped>
  .dealer-list {
    padding: 0.36rem 0.27rem;
  }

  .tips {
    margin-top: 0.9rem;
    text-align: center;
  }

  .padding {
    padding: 0 0.21rem;

    .about-search {
      position: relative;
      width: 100%;
      height: 0.81rem;
      margin: 0.18rem 0;
      border: 1px solid #000;
      box-sizing: border-box;

      form {
        width: 100%;
        height: 100%;
      }

      .city-name {
        display: block;
        width: 100%;
        height: 100%;
        padding-left: 1em;
        box-sizing: border-box;
      }

      .search-store {
        position: absolute;
        top: 0;
        right: 0;
        width: 1.9rem;
        text-align: center;
        padding-left: 0.27rem;
        height: 100%;
        background: url(../../assets/img/f_search.png) no-repeat center left 0.45rem #2e2e2e;
        color: #fff;
        outline: none;
      }
    }
  }

  .news-li {
    a {
      display: flex;
      padding: 0.36rem 0.27rem;
      color: #000;

      .news-li-hd img {
        width: 2.17rem;
        height: 1.63rem;
      }

      .news-li-bd {
        margin-left: 0.18rem;

        .news-li-bd_title {
          font-weight: bold;
          font-size: 0.28rem;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          line-clamp: 2;
          -webkit-box-orient: vertical;
        }

        .news-li-bd_desc {
          color: #6d6d6d;
          font-size: 0.25rem;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          line-clamp: 2;
          -webkit-box-orient: vertical;
        }
      }
    }
  }

  .product-list {
    display: flex;
    display: -webkit-flex;
    justify-content: space-between;
    flex-direction: row;
    flex-wrap: wrap;
    padding: 0.36rem 0.21rem;

    .product-li {
      width: 48%;
      margin-bottom: 0.45rem;

      img.product-img {
        width: 100%;
        height: 4.8rem;
      }

      .stock {
        margin-left: 0.36rem;
      }

      .product-desc {
        display: flex;
        align-items: center;
        justify-content: space-between;

        .wid {
          display: block;
          width: 80%;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }

    }
  }

  .product-type-sign {
    display: flex;
    padding-bottom: 0.27rem;

    >div {
      display: flex;
      align-items: center;
      margin-right: 0.36rem;
    }
  }

  .black {
    display: inline-block;
    width: 0.25rem;
    height: 0.25rem;
    background: #000;
    margin-right: 0.09rem;
  }

  .orange {
    display: inline-block;
    width: 0.25rem;
    height: 0.25rem;
    background: #dd8821;
    margin-right: 0.09rem;
  }

  .green {
    display: inline-block;
    width: 0.25rem;
    height: 0.25rem;
    background: #6fa027;
    margin-right: 0.09rem;
  }

  .stock {
    display: inline-block;
    width: 0.18rem;
    height: 0.18rem;
    background: #000;
    margin-right: 0.09rem;
    border-radius: 50%;
  }

  .case-list {
    padding: 0.36rem 0.27rem;

    .case-list-li {
      margin-bottom: 0.36rem;

      a {
        display: block;
        font-size: 0;
      }

      .case-li_title {
        margin-top: 0.09rem;
      }

      .case-li_img {
        width: 100%;
        height: 3.79rem;
      }
    }
  }
</style>
